<?php
use OSS\OssClient;

class IndexController extends Controller {

	public function indexAction() {
	    Response::redirect("/Admin_index/index");
		exit('');
	}

       private function lockAction() {
        File::writeFile(APP_ROOT.'/public/lock.inc', 'lock');
//        Cache_File::getInstance()->set("lock", 1, 86400*365);
        echo 'lock system success';
    }
    
    private function unlockAction() {
        File::deleteFile(APP_ROOT.'/public/lock.inc');
//        Cache_File::getInstance()->set("lock", 0, 86400*365);
        echo 'unlock system success';
    }     

	public function changeAction() {
		set_time_limit(0);
		$list = self::model('dynamic')->getAll();
		foreach ($list as $row) {
			if ($row['image_list']) {
				$data = [];
				$imageList = explode(',', $row['image_list']);
				$data['image_list'] = implode('$$$', $imageList);
				self::model('dynamic')->update($data, 'dynamic_id=?', $row['dynamic_id']);
				echo $row['image_list'] . "\r\n";
			}
		}
	}

	//https://oss.chongshehui.com/image/wK/uU/xGrd2zbRrW.jpgZ64nfh3jf6.jpg
	public function copyAction() {
		$ossClient = new OSS\OssClient(Configure::get('oss.key'), Configure::get('oss.secret'),
			Configure::get('oss.goto'), false);
		$result = $ossClient->copyObject(Configure::get('oss.bucket'), 'image/wK/uU/xGrd2zbRrW.jpgZ64nfh3jf6.jpg',
			Configure::get('oss.bucket'), 'video/1234.jpg');
		Response::dump($result);
	}

	public function insertUserAction() {
		for ($i = 1; $i <= 100; $i++) {
			$data = [
				'nickname' => uniqid(),
				'avatar' => 'https://image.uc.cn/o/wemedia/s/upload/1705221435d3c824249cc9d00318ca85768c3bdcc0',
			];
			$uid = self::model('user')->insert($data, true);
			$t = mt_rand(0, 100);
			if ($t < 30) {
				self::model('user_relate')->insert([
					'uid' => 1,
					'to_uid' => $uid,
					'add_time' => time(),
				]);
				self::model('user_relate')->insert([
					'uid' => $uid,
					'to_uid' => 1,
					'add_time' => time(),
				]);
			} elseif ($t <= 60) {
				self::model('user_relate')->insert([
					'uid' => 1,
					'to_uid' => $uid,
					'add_time' => time(),
				]);
			} else {
				self::model('user_relate')->insert([
					'uid' => $uid,
					'to_uid' => 1,
					'add_time' => time(),
				]);
			}
		}

	}

	public function tAction() {
		$callback = 'js<script>alert(1)<%2fscript>';
		if (preg_match('#^[^0-9][0-9a-zA-Z._]+$#', $callback)) {
			echo 'success';
		} else {
			echo 'fail';
		}
	}

	public function arrAction() {

	}

	public function ticketAction() {
		$user_info = [];
		$user_info['uid'] = Request::get('uid');
		$ticket = self::model('user')->login($user_info);
		echo $ticket . '<br/>';
		echo '<br/>';
		$Encrypt = Doit::singleton('Encrypt');
		$user_info = array(
			'uid' => '',
			'logintime' => '',
		);
		$key = md5(Configure::get('security_key') . 'user');
		$idString = $Encrypt->decode($ticket, $key);
		if (isset($idString) && $idString && strpos($idString, '\t')) {
			list($user_info['uid'], $user_info['logintime']) = explode('\t', $idString);
			if ($user_info['uid']) {
				var_dump($user_info);
			}
		}

		echo 'over';
	}

	public function flushAction() {
		Cache_Memcache::getInstance()->clear();
		echo 'success';
	}

	public function mcAction() {
		$key = Request::get('key');
		$value = Request::get('value');
		var_dump(Cache_Memcache::getInstance()->set($key, $value));
	}

	public function returnAction() {
		$result = self::model('point_rule')->getOne('id=?', 1);
		Response::ajax(200, 'success', $result['is_show']);
	}

}
